/*
 * Copyright (c) 2018. 玺得（深圳）科技有限公司.保留所有权利. http://www.pi-top.com/ 玺得（深圳）科技有限公司保留所有代码著作权.如有任何疑问请访问官方网站与我们联系. 代码只针对特定需求定制编码，不得在未经允许或授权的情况下对外传播扩散.恶意传播者，法律后果自行承担. 本代码仅用于pps-cloud系统.
 */
package com.pitop.serial.service;

import com.pitop.core.entity.Page;
import com.pitop.core.exceptions.BaseException;
import com.pitop.core.exceptions.SerialNumberException;
import com.pitop.serial.dao.SerialNumberDAO;
import com.pitop.serial.entity.SerialNumberState;
import com.pitop.serial.entity.SerialNumber;
import com.pitop.core.base.BaseDAO;
import com.pitop.core.base.BaseSVImpl;
import com.alibaba.fastjson.serializer.SerializerFeature;
import lombok.extern.slf4j.Slf4j;
import com.alibaba.fastjson.JSON;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.HashMap;
import java.util.Date;
import java.util.Map;

import org.apache.ibatis.session.RowBounds;
import com.baidu.fsg.uid.UidGenerator;

@Service("serialNumberSV")
@Slf4j
public class SerialNumberSVImpl extends BaseSVImpl<SerialNumber, Long> implements SerialNumberSV {

    @Autowired
    private SerialNumberDAO serialNumberDAO;

    @Resource
    private UidGenerator uidGenerator;

    @Override
    protected BaseDAO getBaseDAO() {
        return serialNumberDAO;
    }

    /**
     * 保存account对象
     *
     * @param entity 实体
     * @throws BaseException
     */
    @Override
    public void save(SerialNumber entity) throws BaseException {
        entity.setCode(String.valueOf(uidGenerator.getUID()));
        super.save(entity);
    }

    /**
     * 加载一个对象SerialNumber
     *
     * @param id id* @param code 编码* @param testUnitCode 测试件编码* @param sn 序列号* @param snCode 序列号类型编码
     * @return SerialNumber
     */
    @Override
    public SerialNumber load(java.lang.Long id, java.lang.String code, java.lang.String testUnitCode, java.lang.String sn, java.lang.String snCode) {
        if (id == null && code == null && testUnitCode == null && sn == null && snCode == null) {
            throw new SerialNumberException(BaseException.BaseExceptionEnum.Illegal_Param);
        }

        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("code", code);
        param.put("testUnitCode", testUnitCode);
        param.put("sn", sn);
        param.put("snCode", snCode);
        return serialNumberDAO.load(param);
    }

    /**
     * 加载一个对象SerialNumber 通过id
     *
     * @param id id
     * @return SerialNumber
     */
    @Override
    public SerialNumber loadById(java.lang.Long id) {
        if (id == null) {
            throw new SerialNumberException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return serialNumberDAO.loadById(id);
    }

    /**
     * 加载一个对象SerialNumber 通过code
     *
     * @param code 编码
     * @return SerialNumber
     */
    @Override
    public SerialNumber loadByCode(java.lang.String code) {
        if (code == null) {
            throw new SerialNumberException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return serialNumberDAO.loadByCode(code);
    }

    /**
     * 加载一个对象SerialNumber 通过testUnitCode
     *
     * @param testUnitCode 测试件编码
     * @return SerialNumber
     */
    @Override
    public SerialNumber loadByTestUnitCode(java.lang.String testUnitCode) {
        if (testUnitCode == null) {
            throw new SerialNumberException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return serialNumberDAO.loadByTestUnitCode(testUnitCode);
    }

    /**
     * 加载一个对象SerialNumber 通过sn
     *
     * @param sn 序列号
     * @return SerialNumber
     */
    @Override
    public SerialNumber loadBySn(java.lang.String sn) {
        if (sn == null) {
            throw new SerialNumberException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return serialNumberDAO.loadBySn(sn);
    }

    /**
     * 加载一个对象SerialNumber 通过snCode
     *
     * @param snCode 序列号类型编码
     * @return SerialNumber
     */
    @Override
    public SerialNumber loadBySnCode(java.lang.String snCode) {
        if (snCode == null) {
            throw new SerialNumberException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return serialNumberDAO.loadBySnCode(snCode);
    }


    /**
     * 根据主键id,oldStates 共同更新 SerialNumber 的状态到newState状态
     *
     * @param id        id
     * @param newState  新状态
     * @param oldStates 旧状态集合
     */
    @Override
    public void updateStateById(java.lang.Long id, SerialNumberState newState, SerialNumberState... oldStates) {
        if (id == null) {
            throw new SerialNumberException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (newState == null) {
            throw new SerialNumberException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (oldStates == null) {
            throw new SerialNumberException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        String[] states = new String[oldStates.length];
        for (int i = 0; i < oldStates.length; i++) {
            states[i] = oldStates[i].name();
        }
        serialNumberDAO.updateStateById(id, newState.name(), states);
    }

    /**
     * 根据主键code,oldStates 共同更新 SerialNumber 的状态到newState状态
     *
     * @param code      编码
     * @param newState  新状态
     * @param oldStates 旧状态集合
     */
    @Override
    public void updateStateByCode(java.lang.String code, SerialNumberState newState, SerialNumberState... oldStates) {
        if (code == null) {
            throw new SerialNumberException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (newState == null) {
            throw new SerialNumberException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (oldStates == null) {
            throw new SerialNumberException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        String[] states = new String[oldStates.length];
        for (int i = 0; i < oldStates.length; i++) {
            states[i] = oldStates[i].name();
        }
        serialNumberDAO.updateStateByCode(code, newState.name(), states);
    }

    /**
     * 根据主键testUnitCode,oldStates 共同更新 SerialNumber 的状态到newState状态
     *
     * @param testUnitCode 测试件编码
     * @param newState     新状态
     * @param oldStates    旧状态集合
     */
    @Override
    public void updateStateByTestUnitCode(java.lang.String testUnitCode, SerialNumberState newState, SerialNumberState... oldStates) {
        if (testUnitCode == null) {
            throw new SerialNumberException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (newState == null) {
            throw new SerialNumberException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (oldStates == null) {
            throw new SerialNumberException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        String[] states = new String[oldStates.length];
        for (int i = 0; i < oldStates.length; i++) {
            states[i] = oldStates[i].name();
        }
        serialNumberDAO.updateStateByTestUnitCode(testUnitCode, newState.name(), states);
    }

    /**
     * 根据主键sn,oldStates 共同更新 SerialNumber 的状态到newState状态
     *
     * @param sn        序列号
     * @param newState  新状态
     * @param oldStates 旧状态集合
     */
    @Override
    public void updateStateBySn(java.lang.String sn, SerialNumberState newState, SerialNumberState... oldStates) {
        if (sn == null) {
            throw new SerialNumberException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (newState == null) {
            throw new SerialNumberException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (oldStates == null) {
            throw new SerialNumberException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        String[] states = new String[oldStates.length];
        for (int i = 0; i < oldStates.length; i++) {
            states[i] = oldStates[i].name();
        }
        serialNumberDAO.updateStateBySn(sn, newState.name(), states);
    }

    /**
     * 根据主键snCode,oldStates 共同更新 SerialNumber 的状态到newState状态
     *
     * @param snCode    序列号类型编码
     * @param newState  新状态
     * @param oldStates 旧状态集合
     */
    @Override
    public void updateStateBySnCode(java.lang.String snCode, SerialNumberState newState, SerialNumberState... oldStates) {
        if (snCode == null) {
            throw new SerialNumberException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (newState == null) {
            throw new SerialNumberException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (oldStates == null) {
            throw new SerialNumberException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        String[] states = new String[oldStates.length];
        for (int i = 0; i < oldStates.length; i++) {
            states[i] = oldStates[i].name();
        }
        serialNumberDAO.updateStateBySnCode(snCode, newState.name(), states);
    }

    /**
     * 根据主键id 更新 SerialNumber 的状态到另一个状态
     *
     * @param id    id
     * @param state 状态
     */
    @Override
    public void updateById(java.lang.Long id, SerialNumberState state) {
        if (id == null) {
            throw new SerialNumberException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        serialNumberDAO.updateById(id, state.name());
    }

    /**
     * 根据主键code 更新 SerialNumber 的状态到另一个状态
     *
     * @param code  编码
     * @param state 状态
     */
    @Override
    public void updateByCode(java.lang.String code, SerialNumberState state) {
        if (code == null) {
            throw new SerialNumberException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        serialNumberDAO.updateByCode(code, state.name());
    }

    /**
     * 根据主键testUnitCode 更新 SerialNumber 的状态到另一个状态
     *
     * @param testUnitCode 测试件编码
     * @param state        状态
     */
    @Override
    public void updateByTestUnitCode(java.lang.String testUnitCode, SerialNumberState state) {
        if (testUnitCode == null) {
            throw new SerialNumberException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        serialNumberDAO.updateByTestUnitCode(testUnitCode, state.name());
    }

    /**
     * 根据主键sn 更新 SerialNumber 的状态到另一个状态
     *
     * @param sn    序列号
     * @param state 状态
     */
    @Override
    public void updateBySn(java.lang.String sn, SerialNumberState state) {
        if (sn == null) {
            throw new SerialNumberException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        serialNumberDAO.updateBySn(sn, state.name());
    }

    /**
     * 根据主键snCode 更新 SerialNumber 的状态到另一个状态
     *
     * @param snCode 序列号类型编码
     * @param state  状态
     */
    @Override
    public void updateBySnCode(java.lang.String snCode, SerialNumberState state) {
        if (snCode == null) {
            throw new SerialNumberException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        serialNumberDAO.updateBySnCode(snCode, state.name());
    }


    /**
     * 删除对象SerialNumber
     *
     * @param id id* @param code 编码* @param testUnitCode 测试件编码* @param sn 序列号* @param snCode 序列号类型编码
     * @return SerialNumber
     */
    @Override
    public void delete(java.lang.Long id, java.lang.String code, java.lang.String testUnitCode, java.lang.String sn, java.lang.String snCode) {
        if (id == null && code == null && testUnitCode == null && sn == null && snCode == null) {
            throw new SerialNumberException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("code", code);
        param.put("testUnitCode", testUnitCode);
        param.put("sn", sn);
        param.put("snCode", snCode);
        serialNumberDAO.delete(param);
    }


    /**
     * 查询SerialNumber分页
     *
     * @param serialNumber 对象
     * @param offset       查询开始行
     * @param limit        查询行数
     * @return List<SerialNumber>
     */
    @Override
    public List<SerialNumber> list(SerialNumber serialNumber, int offset, int limit) {
        if (offset < 0) {
            offset = 0;
        }

        if (limit < 0) {
            limit = Page.limit;
        }

        Map<String, Object> map = null;
        if (serialNumber != null) {
            map = JSON.parseObject(JSON.toJSONString(serialNumber, SerializerFeature.WriteDateUseDateFormat));
        } else {
            map = new HashMap<>();
        }
        return serialNumberDAO.list(map, new RowBounds(offset, limit));
    }

    @Override
    public int count(SerialNumber serialNumber) {
        Map<String, Object> map;
        if (serialNumber != null) {
            map = JSON.parseObject(JSON.toJSONString(serialNumber, SerializerFeature.WriteDateUseDateFormat));
        } else {
            map = new HashedMap();
        }
        return serialNumberDAO.count(map);
    }

    /**
     * 查询SerialNumber分页
     *
     * @param id     id* @param code  编码* @param testUnitCode  测试件编码* @param sn  序列号* @param snCode  序列号类型编码
     * @param offset 查询开始行
     * @param limit  查询行数
     * @return List<SerialNumber>
     */
    @Override
    public List<SerialNumber> list(java.lang.Long id, java.lang.String code, java.lang.String testUnitCode, java.lang.String sn, java.lang.String snCode, int offset, int limit) {
        if (offset < 0) {
            offset = 0;
        }

        if (limit < 0) {
            limit = Page.limit;
        }
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("code", code);
        param.put("testUnitCode", testUnitCode);
        param.put("sn", sn);
        param.put("snCode", snCode);
        return serialNumberDAO.list(param, new RowBounds(offset, limit));
    }

    @Override
    public int count(java.lang.Long id, java.lang.String code, java.lang.String testUnitCode, java.lang.String sn, java.lang.String snCode) {
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("code", code);
        param.put("testUnitCode", testUnitCode);
        param.put("sn", sn);
        param.put("snCode", snCode);
        return serialNumberDAO.count(param);
    }

}
